package com.sojson.project.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sojson.project.dao.AdDao;
import com.sojson.project.entity.po.Ad;
import com.sojson.project.entity.vo.AdVo;
import com.sojson.project.service.AdService;
import com.sojson.util.file.constant.ConstantFileFieldName;
import com.sojson.util.file.depot.FileDepotUtil;
import com.sojson.util.file.enums.EFileTable;

/**
 * 广告表Service实现类
 * 
 * @author liu
 * @date 2021-03-16
 */
@Service
public class AdServiceImpl implements AdService {

    @Resource
    private AdDao adDaoImpl;

    /**
     * 单条插入
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public int insert(Ad ad) {
        int insert = adDaoImpl.insert(ad);

        if (insert > 0) {
            FileDepotUtil.insertSync(EFileTable.AD, ConstantFileFieldName.CONTENT, ad.getContent());
        }
        return insert;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public int updatePartialById(Ad ad) {
        Ad findById = adDaoImpl.findById(ad.getId());

        int updatePartialById = adDaoImpl.updatePartialById(ad);

        if (updatePartialById > 0) {
            FileDepotUtil.updateSync(EFileTable.AD.getCode(), ConstantFileFieldName.CONTENT, ad.getContent(),
                findById.getContent());
        }
        return updatePartialById;
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByIdList(List<Integer> ids) {
        List<Ad> findByIdList = adDaoImpl.findByIdList(ids);

        int deleteByIdList = adDaoImpl.deleteByIdList(ids);

        if (deleteByIdList > 0) {
            List<String> orderNoList = findByIdList.stream().map(Ad::getContent).collect(Collectors.toList());
            FileDepotUtil.deleteListSync(orderNoList);
        }
        return deleteByIdList;
    }

    /**
     * 批量插入
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public int insertList(Ad ad) {
        int insertList = adDaoImpl.insertList(ad.getAds());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByDto(Ad ad) {
        int deleteByDto = adDaoImpl.deleteByDto(ad);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public int countAll() {
        int countAll = adDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public List<Ad> findAll() {
        List<Ad> findAll = adDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public int countByDto(Ad ad) {
        int countByDto = adDaoImpl.countByDto(ad);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public List<Ad> findByDto(Ad ad) {
        List<Ad> findByDto = adDaoImpl.listByDto(ad);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = adDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public int updateById(Ad ad) {
        int updateById = adDaoImpl.updateById(ad);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticByIdList(Ad ad) {
        int updateStaticByIdList = adDaoImpl.updateStaticByIdList(ad);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangeByIdList(Ad ad) {
        int updateChangeByIdList = adDaoImpl.updateChangeByIdList(ad.getAds());
        return updateChangeByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticPartialByIdList(Ad ad) {
        int updateStaticPartialByIdList = adDaoImpl.updateStaticPartialByIdList(ad);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangePartialByIdList(Ad ad) {
        int updateChangePartialByIdList = adDaoImpl.updateChangePartialByIdList(ad.getAds());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public Ad findById(String id) {
        Ad findById = adDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Ad> findByIdList(ArrayList<Integer> ids) {
        ArrayList<Ad> findByIdList = adDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Integer> findIdByDto(Ad ad) {
        ArrayList<Integer> findIdByDto = adDaoImpl.findIdByDto(ad);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     * 
     * @throws SQLException
     */
    @Override
    public void clear() {
        adDaoImpl.clear();
    }

    /**
     * 根据对象查询
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @Override
    public List<Ad> findAppByDto(AdVo ad) {
        return adDaoImpl.findAppByDto(ad);
    }

}